﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PMSSys
{
    public partial class BoneMarrowList : Form
    {
        public BoneMarrowList(patientController ppcontroller, Patient pObject)
        {
            InitializeComponent();
            bmController = new bmaController();
            bmController.DbConnect = ppcontroller.DbConnect;
            this.pObject = pObject;
            DataTable lst = bmController.getBMADataTable(pObject.PatientID);
            dataGridView1.DataSource = lst;
            dataGridView1.Columns[0].Visible = false;
        }
        private bmaController bmController;
        private Patient pObject;

        private void newBMReadingButton_Click(object sender, EventArgs e)
        {          
            BoneMarrowReadingView newWindow = new BoneMarrowReadingView(bmController, String.Empty, pObject.PatientID );
            newWindow.ShowDialog();
            DataTable lst = bmController.getBMADataTable(pObject.PatientID);
            dataGridView1.DataSource = lst;
            dataGridView1.Columns[0].Visible = false;
        }

        private void openBMReadingFileButton_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount != 0)
            {
                String id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                BoneMarrowReadingView newWindow = new BoneMarrowReadingView(bmController, id, pObject.PatientID);
                newWindow.ShowDialog();
                DataTable lst = bmController.getBMADataTable(pObject.PatientID);
                dataGridView1.DataSource = lst;
                dataGridView1.Columns[0].Visible = false;
            }
        }

        private void removeMarkerButton_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount != 0)
            {
                String id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                DateTime date = DateTime.Parse(dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
                string str = "Remove entry for " + date.DayOfWeek + " " + date.Day + " " + date.ToString("MMMM") + " " + date.Year.ToString() + "?";
                DialogResult res = uiUtility.confirmDelete(str);
                if (res == DialogResult.Yes)
                {
                    bmController.delete(id);
                    DataTable lst = bmController.getBMADataTable(pObject.PatientID);
                    dataGridView1.DataSource = lst;
                    dataGridView1.Columns[0].Visible = false;
                }
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BoneMarrowList_Load(object sender, EventArgs e)
        {
            ToolTip t1 = uiUtility.tooltips();
            t1.SetToolTip(this.newBMReadingButton, "New Bone Marrow Reading");
            t1.SetToolTip(this.openBMReadingFileButton, "Open Bone Marrow Reading");
            t1.SetToolTip(this.removeMarkerButton, "Remove Bone Marrow Reading");
            t1.SetToolTip(this.button6, "Back");
        }
        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (dataGridView1.RowCount == 0)
            {
                openBMReadingFileButton.Enabled = false;
                removeMarkerButton.Enabled = false;
            }
            else
            {
                openBMReadingFileButton.Enabled = true;
                removeMarkerButton.Enabled = true;
            }
        }
    }
}
